package com.zy.controller;

import com.zy.common.DataGridView;
import com.zy.common.Menu;
import com.zy.common.tree.TreeNode;
import com.zy.shiro.domain.SysMenu;
import com.zy.shiro.domain.SysUser;
import com.zy.shiro.service.SysMenuService;
import com.zy.shiro.service.SysUserService;
import com.zy.vo.MenuAuthResult;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys/menu")
public class MenuController {
  @Autowired
  private SysMenuService sysMenuService;

  @Autowired
  private SysUserService sysUserService;

  /**
   * 加载用户的菜单和权限数据
   * @return
   */
  @GetMapping("/nav")
  public ResponseEntity<MenuAuthResult> loadMenuAndAuth() {
    SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
    MenuAuthResult menuAuthResult = sysMenuService.loadMenuAndAuth(sysUser.getUserId());
    return ResponseEntity.ok(menuAuthResult);
  }
  /**
   * 加载用户的菜单数据
   *
   */
  @GetMapping("/loadIndexMenuJson")
  public DataGridView loadIndexMenuJson(){
    //1,得到当前用户是什么类型
    Subject subject = SecurityUtils.getSubject();
//    ActiveUser activeUser= (ActiveUser) SecurityUtils.getSubject().getPrincipal();
    SysUser sysUser= (SysUser) subject.getPrincipal();
//    SysUser sysUser = sysUserService.getById(1);
    if(null!=sysUser){//如果activerUser为空说明当前访问者没有登陆
      List<SysMenu> menuList=null;
      menuList=this.sysMenuService.queryeMenuByUserIdForList(sysUser.getUserId());
      List<TreeNode> treeNodes=new ArrayList<>();
      for (SysMenu m : menuList) {
//        Boolean spread=m.getSpread()==1?true:false;
//        treeNodes.add(new TreeNode(m.getId(),m.getPid(),m.getTitle(),m.getIcon(),m.getHref(),m.getTarget(),spread));
          treeNodes.add(new TreeNode(m.getMenuId().intValue(),m.getParentId().intValue(),m.getMenuName(),m.getIcon(),m.getUrl(),"_self",Boolean.TRUE));
      }
      DataGridView dataGridView = new DataGridView(TreeNode.TreeNodeBuilder.build(treeNodes, 0));
      return  dataGridView;
    }else{
      return new DataGridView(-1,null);
    }
  }

//  }
//  @GetMapping("/table")
//  @ApiOperation("加载所有的菜单列表")
//  @RequiresPermissions(value = "sys:menu:list")
//  public ResponseEntity<List<SysMenu>> loadAllMenuList(){
//    List<SysMenu> list = sysMenuService.list();
//    return  ResponseEntity.ok(list);
//  }
//
//  @GetMapping("/list")
//  @ApiOperation("查询父菜单")
//  @RequiresPermissions("sys:menu:list")
//  public  ResponseEntity<List<SysMenu>> loadParentMenuList(){
//    List<SysMenu> sysMenus = sysMenuService.listParentList();
//    return  ResponseEntity.ok(sysMenus);
//  }
//  @ApiOperation("新增一个菜单")
//  @PostMapping
//  public  ResponseEntity<Void> add(@RequestBody @Validated SysMenu sysMenu){
//    sysMenuService.save(sysMenu);
//    return  ResponseEntity.ok().build();
//
//  }
//  @ApiOperation("删除一个菜单")
//  @RequiresPermissions("sys:menu:delete")
//  @DeleteMapping("/{id}")
//  public ResponseEntity<Void> delete(@PathVariable("id") Long id){
//    sysMenuService.removeById(id);
//    return  ResponseEntity.ok().build();
//  }
//  @ApiOperation("数据的回显")
//  @RequiresPermissions("sys:menu:info")
//  @GetMapping("/info/{id}")
//  public  ResponseEntity<SysMenu> findById(@PathVariable("id") Long id){
//    SysMenu byId = sysMenuService.getById(id);
//    return  ResponseEntity.ok(byId);
//  }
//  @PutMapping
//  @ApiOperation("修改数据")
//  @RequiresPermissions("sys:menu:update")
//  public  ResponseEntity<Void> update(@RequestBody @Validated SysMenu sysMenu){
//    sysMenuService.updateById(sysMenu);
//    return  ResponseEntity.ok().build();
//
//  }

}